import {createSlice, createAsyncThunk} from "@reduxjs/toolkit";


export const asyncAdd = createAsyncThunk("asyncAdd", (value) => {
    return value
})

export const asyncSub = createAsyncThunk("asyncSub", (value) => {
    return value
})

const countStore = createSlice({
    name: "countStore",
    initialState: {
        count: 0,
    },
    reducers: {
        add(state, action) {
            state.count += action.payload;
        },
        sub(state, action) {
            state.count -= action.payload;
        }
    },
    extraReducers(builder) {
        builder.addCase(asyncSub.pending, (state, action) => {
            console.log("pending", state, action)
        }).addCase(asyncSub.fulfilled, (state, action) => {
            console.log("fulfilled", state, action)
            state.count += action.payload
        }).addCase(asyncSub.rejected, (state, action) => {
            console.log("rejected", state, action)
        })
        builder.addCase(asyncAdd.pending, (state, action) => {
            console.log("pending", state, action)
        }).addCase(asyncAdd.fulfilled, (state, action) => {
            console.log("fulfilled", state, action)
            state.count += action.payload
        }).addCase(asyncAdd.rejected, (state, action) => {
            console.log("rejected", state, action)
        })
    }
})

export const {add, sub} = countStore.actions

export default countStore.reducer
